package org.zjvis.datascience.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.zjvis.datascience.common.dto.ModelLocalDTO;
import org.zjvis.datascience.common.enums.InstallStatusEnum;
import org.zjvis.datascience.common.model.ApiResult;
import org.zjvis.datascience.common.model.ApiResultCode;
import org.zjvis.datascience.common.util.DozerUtil;
import org.zjvis.datascience.common.util.JwtUtil;
import org.zjvis.datascience.common.util.RestTemplateUtil;
import org.zjvis.datascience.service.CaseService;
import org.zjvis.datascience.service.ModelLocalService;
import org.zjvis.datascience.service.mapper.CaseMapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/modelOnline")
@RestController
@Api(tags = "modelOnline", description = "线上模型接口")
@Validated
public class ModelOnlineController {

    private final static Logger logger = LoggerFactory.getLogger("ModelOnlineController");

    @Autowired
    private RestTemplateUtil restTemplateUtil;
    @Autowired
    private ModelLocalService modelLocalService;

    @Autowired
    private CaseMapper caseMapper;
    private CaseService caseService;
    @PostMapping(value = "/create")
    @ResponseBody
    @ApiOperation(value = "添加模型", notes = "添加新建模型")
    public ApiResult<Long> create(@RequestBody JSONObject params) {
        return restTemplateUtil.submitMOJob(params, "create");
    }

    @PostMapping(value = "/createVersion")
    @ResponseBody
    @ApiOperation(value = "添加模型新版本", notes = "添加模型新版本")
    public ApiResult<Long> createVersion(HttpServletRequest request, @RequestBody JSONObject params) {
        return restTemplateUtil.submitMOJob(params, "createVersion");
    }

    @PostMapping(value = "/update")
    @ResponseBody
    @ApiOperation(value = "更新模型", notes = "更新模型信息")
    public ApiResult<Void> update(HttpServletRequest request, @RequestBody JSONObject params) {
        return restTemplateUtil.submitMOJob(params, "update");
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    @ApiOperation(value = "删除模型", notes = "删除模型文件及模型信息")
    public ApiResult<Void> delete(HttpServletRequest request, @RequestBody JSONObject params) {
        return restTemplateUtil.submitMOJob(params, "delete");
    }

    @PostMapping(value = "/query")
    @ResponseBody
    @ApiOperation(value = "搜索模型", notes = "搜索模型，支持模糊搜索")
    public ApiResult<JSONObject> query(HttpServletRequest request, @RequestBody JSONObject params) {
        params.put("userId", JwtUtil.getCurrentUserId());
        JSONObject ret = modelLocalService.queryOnline(params);
        return ApiResult.valueOf(ret);
    }

    @PostMapping(value = "/queryById")
    @ResponseBody
    @ApiOperation(value = "根据id搜索模型", notes = "根据id搜索模型")
    public ApiResult<JSONObject> queryById(HttpServletRequest request,
        @RequestBody JSONObject params) {
        params.put("userId", JwtUtil.getCurrentUserId());
        JSONObject ret = modelLocalService.queryByIdOnline(params);
        return ApiResult.valueOf(ret);
    }

    @PostMapping(value = "/queryByUid")
    @ResponseBody
    @ApiOperation(value = "查询模型版本", notes = "查询模型版本")
    public ApiResult<JSONArray> queryByUid(HttpServletRequest request,
        @RequestBody JSONObject params) {
        return restTemplateUtil.submitMOJob(params, "queryByUid");
    }

    @PostMapping(value = "/install")
    @ResponseBody
    @ApiOperation(value = "安装模型", notes = "安装模型")
    public ApiResult<Long> install(HttpServletRequest request, @RequestBody JSONObject params) {
        return modelLocalService.install(params);
    }

    @PostMapping(value = "/watch")
    @ResponseBody
    @ApiOperation(value = "浏览模型", notes = "浏览模型")
    public ApiResult<Void> watch(HttpServletRequest request, @RequestBody JSONObject params) {
        return restTemplateUtil.submitMOJob(params, "watch");
    }

    @PostMapping(value = "/favor")
    @ResponseBody
    @ApiOperation(value = "收藏模型", notes = "收藏模型")
    public ApiResult<Void> favor(HttpServletRequest request, @RequestBody JSONObject params) {
        return restTemplateUtil.submitMOJob(params, "favor");
    }

    @PostMapping(value = "/extractKeywords")
    @ResponseBody
    @ApiOperation(value = "提取关键词", notes = "提取关键词")
    public ApiResult<JSONArray> extractKeywords(HttpServletRequest request, @RequestBody JSONObject params){
        return restTemplateUtil.submitMOJob(params, "extractKeywords");
    }

    @PostMapping(value = "/statistics")
    @ResponseBody
    @ApiOperation(value = "模型统计", notes = "模型统计")
    public ApiResult<JSONObject> statistics(HttpServletRequest request, @RequestBody JSONObject params) {
        return restTemplateUtil.submitMOJob(params, "statistics");
    }

    @PostMapping(value = "/loadPanel")
    @ResponseBody
    @ApiOperation(value = "加载panel", notes = "加载panel")
    public ApiResult<JSONObject> loadPanel(HttpServletRequest request, @RequestBody JSONObject params) {
        return restTemplateUtil.submitMOJob(params, "loadPanel");
    }

    @GetMapping(value = "/download")
    @ResponseBody
    @ApiOperation(value = "下载模型", notes = "下载模型")
    public ApiResult<Void> download(HttpServletResponse response, @RequestParam Long id){
        try {
            return modelLocalService.moDownload(response, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ApiResult.valueOf(ApiResultCode.DOWNLOAD);
    }


    @GetMapping(value = "/test")
    @ResponseBody
    @ApiOperation(value = "test", notes = "test")
    public ApiResult<JSONObject> test(HttpServletResponse response) {
        String key = "39da746b37f2f1485d7eddcc410dd91a";
        String url = String
            .format("https://restapi.amap.com/v3/geocode/regeo?location=116.310003,39.991957|116.310003,39.991957&batch=true&&key=%s",
                key);
        JSONObject result = JSONObject.parseObject(restTemplateUtil.get(url));
        return ApiResult.valueOf(result);
    }
    @PostMapping (value = "/newCaseAndMost")
    @ResponseBody
    @ApiOperation(value = "获取最多最新", notes = "获取最多最新")
    public ApiResult<JSONObject> newCase(){

        JSONObject newcase = new JSONObject();
        newcase = modelLocalService.newCaseAndMost(JwtUtil.getCurrentUserId());
        return ApiResult.valueOf(newcase);
    }
    @PostMapping(value = "/queryCase")
    @ResponseBody
    @ApiOperation(value = "搜索模型", notes = "搜索模型，支持模糊搜索")
    public ApiResult<JSONObject> queryCase(@RequestBody JSONObject params) {
        if (StringUtils.isEmpty(params.getString("pageIndex")) || StringUtils
                .isEmpty(params.getString("pageSize"))) {
            return ApiResult.valueOf(ApiResultCode.PARAM_ERROR);
        }
        JSONObject specialcase = new JSONObject();
        specialcase  = modelLocalService.queryCase(params);
        return ApiResult.valueOf(specialcase);
    }

    @PostMapping(value = "/addVideo")
    @ApiOperation(value = "加视频播放", notes = "加视频播放")
    public ApiResult<JSONObject> addVideo( @RequestBody JSONObject params){

        return restTemplateUtil.submitMOJob(params, "addVideoNebula");
    }




}